package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import model.Point;

public class TextDisplayer extends ShapeDisplayerJFxImpl
{
    public static final int DEFAULT_FONT_SIZE = 20;
    public static final Point DEFAULT_LOC = new Point(100,100);

    public static final String SELECTED_FONT_FAMILY = "Verdana";
    public static final FontPosture SELECTED_FONT_POSTURE = FontPosture.ITALIC ;


    private Text text;

    TextDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Text();
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        text = (Text)shape;
        text.setFont(new Font(DEFAULT_FONT_SIZE));
        text.setX(DEFAULT_LOC.getX());
        text.setY(DEFAULT_LOC.getY());
    }

    public void setContent(String content) { text.setText(content); }

    public String getContent() { return text.getText(); }

    @Override
    public void setColor(Color color) { text.setFill(color); }

    @Override
    public Color getColor(){return (Color)text.getFill();}

    @Override
    public void setSelected(boolean isSelected)
    {
        double fontSize = text.getFont().getSize();
        if(isSelected)
            text.setFont(Font.font(SELECTED_FONT_FAMILY,SELECTED_FONT_POSTURE,fontSize));
        else
            text.setFont(new Font(fontSize));
    }

    @Override
    public void setOutStanding(boolean isOutStanding) { text.setUnderline(isOutStanding); }

    @Override
    public ShapeDisplayer clone()
    {
        TextDisplayer copy = new TextDisplayer(this.canvas,this.controller);
        copy.text.setText(this.text.getText());
        copy.text.setX(this.text.getX());
        copy.text.setY(this.text.getY());
        copy.text.setFont(new Font(this.text.getFont().getSize()));
        copy.text.setFill(this.text.getFill());
        return copy;
    }

    @Override
    public void setLocation(Point start, Point end) {}

    @Override
    protected void resize(double multi)
    {
        double fontSize = text.getFont().getSize();
        fontSize *= (1 + multi);
        text.setFont(new Font(fontSize));
    }

    @Override
    protected Point getCenter()
    {
        Point center = new Point(text.getX(),text.getY());
        return center;
    }

    @Override
    protected void setCenter(Point center)
    {
        text.setX(center.getX());
        text.setY(center.getY());
    }

}
